package com.example.demo.vo;


import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jack
 * @Title: 分页结构体
 * @Description: 分页结构体
 * @date 2018年1月7日14:36:35
 */
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 7634833622872685023L;
    /* 当前页 */
    private int currentPage = 1;
    /* 下一页 */
    private int nextPage = 1;
    /* 上一页 */
    private int prePage = 1;
    /* 开始条数 */
    private int offset = 0;
    /* 每页显示记录数 */
    private int limit = 10;
    /* 总记录数 */
    private int totalCount;
    /* 一共多少页 */
    private int totalPage;
    /* 结果集 */
    private List<T> result = new ArrayList<>();

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage <= 0 ? 1 : currentPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage() {
        this.nextPage = this.currentPage + 1 >= this.totalPage ? totalPage : this.currentPage + 1;
    }

    public int getPrePage() {
        return prePage;
    }

    public void setPrePage() {
        this.prePage = this.currentPage - 1 <= 0 ? 1 : this.currentPage - 1;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage() {
        this.totalPage = this.totalCount / limit == 0 ? this.totalCount / limit : this.totalCount / limit + 1;
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    public PageResult(PageRequest pageRequest, List<T> resultList, int totalCount) {
        this.setCurrentPage(pageRequest.getCurrentPage());
        this.setOffset(pageRequest.getOffset());
        this.setLimit(pageRequest.getLimit());
        this.setTotalCount(totalCount);
        this.setResult(resultList);
        this.setPrePage();
        this.setTotalPage();
        this.setNextPage();

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PageResult<?> that = (PageResult<?>) o;

        if (currentPage != that.currentPage) return false;
        if (nextPage != that.nextPage) return false;
        if (prePage != that.prePage) return false;
        if (offset != that.offset) return false;
        if (limit != that.limit) return false;
        if (totalCount != that.totalCount) return false;
        if (totalPage != that.totalPage) return false;
        return result != null ? result.equals(that.result) : that.result == null;
    }

    @Override
    public int hashCode() {
        int result1 = currentPage;
        result1 = 31 * result1 + nextPage;
        result1 = 31 * result1 + prePage;
        result1 = 31 * result1 + offset;
        result1 = 31 * result1 + limit;
        result1 = 31 * result1 + totalCount;
        result1 = 31 * result1 + totalPage;
        result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
        return result1;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}


